# -*- coding: utf-8 -*-
# @Date：2024/9/30 11:53:07
# @Author：CJiaの青姝
# @FileName：cj_auth
# @Editor：PyCharm2024
# @Remark：
from ninja.security import HttpBearer

from utils.cj_jwt import decode_token


class GlobalAuth(HttpBearer):
    """ 全局身份认证 """

    def authenticate(self, request, token):
        # token解密
        payload = decode_token(token)
        # 验证成功
        if payload['code'] == 0:
            request.user_id = payload['data']['id']
            # 验证成功
            return token
        else:
            # token异常
            raise TimeoutError(401, payload['msg'])